﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Vms.DB;
using Vms.Business;
using System.IO;
using System.Text;

public partial class TuVi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string birthday = "";
        string gioitinh = "";
        DB m_db = new DB();
        Util m_util = new Util();
        WriteFile wf = new WriteFile();
        Util.TSession m_tSession = new Util.TSession();
        try
        {
            m_util.write_log(Request.Url.OriginalString, 2);
            m_util.WritelogHeader(Request.Headers);
            string str = "";
            int service_id = 0;
            str = "<?xml version='1.0' encoding='ISO-8859-1'?>" +
                   "<!DOCTYPE pages SYSTEM 'cellflash.dtd'>" +
                   "<pages descr='Samis'>" +
                   "<page> ";
            if (Request.Params["birthday"] != null)
            {
                birthday = Request.Params["birthday"];
            }
            if (Request.Params["gioitinh"] != null)
            {
                gioitinh = Request.Params["gioitinh"];
            }
            if (Request.Params["sid"] != null)
            {
                service_id = Int32.Parse(Request.Params["sid"]);
            }
            else
            {
                m_tSession.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
                m_tSession.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
                m_tSession.ServiceList = "5";
                m_tSession.KeyPress = "5";
                m_tSession.SessionID = "";
                m_tSession.IP = "";
                m_db.UpdateReQuestTbl(m_tSession);
                service_id = Int32.Parse(Request.Params["cid"]);
                DataSet ds = new DataSet();
                ds = m_db.GetServiceByPage(service_id);
                DataTable dtb = new DataTable();
                dtb = ds.Tables[0].Clone();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    dtb.Rows.Add(ds.Tables[0].Rows[i].ItemArray);
                }
                foreach (DataRow dr in dtb.Rows)
                {
                    str += "<a href='" + dr["URL"].ToString().Trim() + dr["Service_ID"].ToString() + "'>" + dr["Text"].ToString().Trim() + "(" + dr["Cost"].ToString() + ")</a><br/>";
                }
            }
            if (service_id == 51)
            {
                //Sim so dep
                m_tSession.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
                m_tSession.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
                m_tSession.ServiceList = "51";
                m_tSession.KeyPress = "1";
                m_tSession.SessionID = "";
                m_tSession.IP = "";
                m_db.UpdateReQuestTbl(m_tSession);
                m_util.write_log(birthday, 1);
                if (!m_util.CheckDateValid(birthday))
                {
                    str += "Nhap ngay sinh cua ban:" +
                        //"<form action='/850/sys/bill?a=" + m_db.GetCostByService(Service_id).ToString() + "&b=0&c=KetQuaXoso&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_id.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_id.ToString())) + ":KQXS&d=Xoso_KetQua.aspx?Tinhid=" + tinh_id + "'>" +
                                "<form action='Tuvi.aspx?sid=51'>" +
                                "<entry kind='digits' var='birthday'>" +
                                "<prompt>(2 so cho ngay, 2 so cho thang va 4 so cho nam(VD 27041986)</prompt>" +
                                "</entry>" +
                                "</form>";
                }
                else if ((gioitinh != "0") && (gioitinh != "1"))
                {
                    str += "Nhap gioi tinh:" +
                        //"<form action='/850/sys/bill?a=" + m_db.GetCostByService(Service_id).ToString() + "&b=0&c=KetQuaXoso&t=0&re=0&i=" + String.Format("{0:000000}", Int32.Parse(Service_id.ToString().Substring(1, 1))) + ":" + String.Format("{0:0000000000}", Int32.Parse(Service_id.ToString())) + ":KQXS&d=Xoso_KetQua.aspx?Tinhid=" + tinh_id + "'>" +
                         "<form action='Tuvi.aspx?sid=51&birthday=" + birthday + "'>" +
                         "<entry kind='digits' var='gioitinh'>" +
                         "<prompt>(0 cho nam, 1 cho nu)</prompt>" +
                         "</entry>" +
                         "</form>";
                }
                else
                {
                    if (gioitinh == "0")
                    {
                        str += "Ban la Nam sinh ngay " + birthday + "</br>" +
                            "<a href='Xoso_KetQua.aspx?service_id";
                    }
                    else
                    {
                        str += "Ban la Nu sinh ngay " + birthday;
                    }
                }
            }
            else if (service_id == 52)
            {
                //Ngay tot
                m_tSession.UserMSISDN = m_util.GetHeaderValue(Request.Headers, "User-MSISDN");
                m_tSession.User_ID = m_util.GetHeaderValue(Request.Headers, "User-UID");
                m_tSession.ServiceList = "52";
                m_tSession.KeyPress = "2";
                m_tSession.SessionID = "";
                m_tSession.IP = "";
                m_db.UpdateReQuestTbl(m_tSession);
            }
            str += "</page>" +
                  "</pages>";
            Response.ClearHeaders();
            Response.AddHeader("content-type", "text/xml");
            Response.Write(str);
            wf.WriteStreamFile(str);
            Response.End();
        }
        catch (Exception ex)
        {
            wf.WriteError(ex.ToString());
        }
    }
}